# 个人中心controller层
from flask import Blueprint, request
import json
import service.PersonalService as PersonalService
from utils import Token

# 创建personal蓝图
personal = Blueprint('personal', __name__)


@personal.before_request
def is_login():
    return Token.verify_auth_token(request)


@personal.after_request
def after_request(response):
    response.headers.add('Access-Control-Allow-Origin', '*')
    response.headers.add('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, HEAD, OPTIONS')
    response.headers.add('Access-Control-Allow-Headers', '*')
    return response


# 用户中心趋势饼图，弃用
@personal.route('/userCenter/areasChart', methods=['GET'])
def personal_areasChart():
    uuid = int(request.args.get('uuid', type=str, default=None))
    result = PersonalService.service_personal_areasChart(uuid)
    return result


# 改昵称
@personal.route('/userCenter/changeNickName', methods=['POST'])
def personal_changeNickName():
    data = request.get_data()
    data = json.loads(data)

    uuid = int(data['uuid'])
    newNickName = data['newNickName']
    result = PersonalService.service_personal_changeNickName(uuid, newNickName)
    return result


# 改头像
@personal.route('/userCenter/changeAvatar', methods=['POST'])
def personal_changeAvatar():
    uuid = int(request.form.get('uuid', type=str, default=None))
    newAvatar = request.files.get('file')
    result = PersonalService.service_personal_changeAvatar(uuid, newAvatar)
    return result


# 用户中心提交记录，弃用
@personal.route('/userCenter/submitLog', methods=['GET'])
def personal_submitLog():
    uuid = int(request.args.get('uuid', type=str, default=None))
    result = PersonalService.service_personal_submitLog(uuid)
    return result


# 提交日历图，弃用
@personal.route('/userCenter/uploadChart', methods=['GET'])
def personal_uploadChart():
    uuid = int(request.args.get('uuid', type=str, default=None))
    # 图表年份，默认2022
    year = int(request.args.get('year', type=str, default='2022'))

    result = PersonalService.service_personal_uploadChart(uuid, year)
    return result


# 删除文章
@personal.route('/userCenter/deleteArticle', methods=['POST'])
def personal_deleteArticle():
    data = request.get_data()
    data = json.loads(data)

    articleId = int(data['articleId'])
    result = PersonalService.service_personal_deleteArticle(articleId)
    return result


# 上传文章
@personal.route('/userCenter/submitArticle', methods=['POST'])
def personal_submitArticle():
    data = request.get_data()
    data = json.loads(data)

    content = data['content']
    uuid = int(data['uuid'])
    title = data['title']
    coverImage = data['coverImage']
    result = PersonalService.service_personal_submitArticle(content, uuid, title, coverImage)
    return result


# 文章修改
@personal.route('/userCenter/changeArticle', methods=['POST'])
def personal_changeArticle():
    data = request.get_data()
    data = json.loads(data)

    content = data['content']
    uuid = int(data['uuid'])
    articleId = int(data['articleId'])
    title = data['title']
    coverImage = data['coverImage']
    result = PersonalService.service_personal_changeArticle(content, uuid, articleId, title, coverImage)
    return result


# 我发布的文章
@personal.route('/userCenter/myArticle', methods=['GET'])
def personal_myArticle():
    # 文章分页
    pagination = request.args.get('pagination', type=int, default=None)
    # 返回文章数量，默认10条
    amount = request.args.get('amount', type=int, default=10)
    # 筛选规则，1 代表按点赞排序，2 代表按时间排序，3 代表按浏览量排序
    rule = request.args.get('rule', type=int, default=None)
    uuid = int(request.args.get('uuid', type=str, default=None))

    result = PersonalService.service_personal_myArticle(pagination, amount, rule, uuid)
    return result
